home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_expect.idb / usr / freeware / bin / passmass.z / passmass
Encoding:
Text File  |  1999-01-26  |  4.6 KB  |  198 lines

  1. #!/usr/freeware/bin/expect --
  2. # passmass: change password on many machines
  3. # Synopsis: passmass host1 host2 host3 ....
  4. # Don Libes - March 11, 1991
  5.  
  6. # Description: Change passwords on the named machines.
  7. #
  8. # You are prompted for old/new passwords.  (If you are changing root
  9. # passwords and have equivalencing, the old password is not used and may be
  10. # omitted.)
  11. # Additional arguments may be used for fine tuning.  They affect all hosts
  12. # which follow until another argument overrides.
  13. #
  14. #   -user    User whose password will be changed.  By default, the current
  15. #        user is used.
  16. #   -rlogin    Use rlogin to access host.  (default)
  17. #   -slogin    Use slogin to access host.
  18. #   -telnet    Use telnet to access host.
  19. #   -program    Next argument is taken as program to run to set password.
  20. #        Default is "passwd".  Other common choices are "yppasswd" and
  21. #        "set passwd" (e.g., VMS hosts).
  22. #   -prompt    Next argument is taken as a prompt suffix pattern.  This allows
  23. #        the script to know when the shell is prompting.  The default is
  24. #        "# " for root and "% " for non-root accounts.
  25. #   -timeout    Next argument is number of seconds to wait for responses.
  26. #        Default is 30 but some systems can be much slower logging in.
  27.  
  28. # The best way to run this is to put the command in a one-line shell script
  29. # or alias. (Presumably, the set of hosts and parameters will rarely change.)
  30. # Then run it whenever you want to change your passwords on all the hosts.
  31.  
  32. exp_version -exit 5.0
  33.  
  34. if $argc==0 {
  35.     send_user "usage: $argv0 host1 host2 host3 . . .\n"
  36.     exit
  37. }
  38.  
  39. expect_before -i $user_spawn_id \003 exit
  40.  
  41. proc badhost {host emsg} {
  42.     global badhosts
  43.  
  44.     send_user "\r\n\007password not changed on $host - $emsg\n\n"
  45.     if 0==[llength $badhosts] {
  46.         set badhosts $host
  47.     } else {
  48.         set badhosts [concat $badhosts $host]
  49.     }
  50. }
  51.  
  52. # set defaults
  53. set login "rlogin"
  54. set program "passwd"
  55. set user [exec whoami]
  56.  
  57. set timeout 1000000
  58. stty -echo
  59. send_user "old password: "
  60. expect_user -re "(.*)\n"
  61. send_user "\n"
  62. set password(old) $expect_out(1,string)
  63. send_user "new password: "
  64. expect_user -re "(.*)\n"
  65. send_user "\n"
  66. set password(new) $expect_out(1,string)
  67. send_user "retype new password: "
  68. expect_user -re "(.*)\n"
  69. set password(newcheck) $expect_out(1,string)
  70. send_user "\n"
  71. stty echo
  72. trap exit SIGINT
  73.  
  74. if ![string match $password(new) $password(newcheck)] {
  75.     send_user "mismatch - password unchanged\n"
  76.     exit
  77. }
  78.  
  79.  
  80. #send_user "want to see new password you just typed? (y|n) "
  81. #expect_user "*\n"
  82. #
  83. #if [string match "y" [lindex $expect_match 0 c]] {
  84. #    send_user "password is <$password(new)>\nproceed? (y|n) "
  85. #    expect_user "*\n"
  86. #    if ![string match "y" [lindex $expect_match 0 c]] exit
  87. #}
  88.  
  89. set timeout 30
  90. set badhosts {}
  91. for {set i 0} {$i<$argc} {incr i} {
  92.  
  93.     set arg [lindex $argv $i]
  94.     switch -- $arg \
  95.         "-user" {
  96.             incr i
  97.             set user [lindex $argv $i]
  98.             continue
  99.         } "-prompt" {
  100.             incr i
  101.             set prompt [lindex $argv $i]
  102.             continue
  103.         } "-rlogin" {
  104.             set login "rlogin"
  105.             continue
  106.         } "-slogin" {
  107.             set login "slogin"
  108.             continue
  109.         } "-telnet" {
  110.             set login "telnet"
  111.             continue
  112.         } "-program" {
  113.             incr i
  114.             set program [lindex $argv $i]
  115.             continue
  116.         } "-timeout" {
  117.             incr i
  118.             set timeout [lindex $argv $i]
  119.             continue
  120.         }
  121.  
  122.     set host $arg
  123.     if [string match $login "rlogin"] {
  124.         set pid [spawn rlogin $host -l $user]
  125.     } elseif [string match $login "slogin"] {
  126.         set pid [spawn slogin $host -l $user]
  127.     } elseif [string match $login "ssh"] {
  128.         set pid [spawn ssh $host -l $user]
  129.     } else {
  130.         set pid [spawn telnet $host]
  131.         expect -re "(login|Username):.*" {
  132.             send "$user\r"
  133.         }
  134.     }
  135.  
  136.     if ![info exists prompt] {
  137.         if [string match $user "root"] {
  138.             set prompt "# "
  139.         } else {
  140.             set prompt "(%|\\\$) "
  141.         }
  142.     }
  143.  
  144.     set logged_in 0
  145.     for {} 1 {} {
  146.         expect "Password*" {
  147.             send "$password(old)\r"
  148.         } eof {
  149.             badhost $host "spawn failed"
  150.             break
  151.         } timeout {
  152.             badhost $host "could not log in (or unrecognized prompt)"
  153.             exec kill $pid
  154.             expect eof
  155.             break
  156.         } -re "incorrect|invalid" {
  157.             badhost $host "bad password or login"
  158.             exec kill $pid
  159.             expect eof
  160.             break
  161.         } -re $prompt {
  162.             set logged_in 1
  163.             break
  164.         }
  165.     }
  166.  
  167.     if (!$logged_in) {
  168.         wait
  169.         continue
  170.     }
  171.  
  172.     send "$program\r"
  173.     expect "Old password*" {
  174.         send "$password(old)\r"
  175.         expect "Sorry*" {
  176.             badhost $host "old password is bad?"
  177.             continue
  178.         } "password:"
  179.     } -re "(n|N)ew password:"
  180.     send "$password(new)\r"
  181.     expect -re "not changed|unchanged" {
  182.         badhost $host "new password is bad?"
  183.         continue
  184.     } -re "(password|Verification|Verify|again):.*"
  185.     send "$password(new)\r"
  186.     expect -re "(not changed|incorrect|choose new).*" {
  187.         badhost $host "password is bad?"
  188.         continue
  189.     } -re "$prompt"
  190.     send_user "\n"
  191.  
  192.     close
  193.     wait
  194. }
  195.  
  196. if [llength $badhosts] {send_user "\nfailed to set password on $badhosts\n"}
  197.